package com.nulldev.util.crypto.api3.encryptors;

import java.nio.charset.Charset;

import com.nulldev.util.crypto.api2.Cryptable;

public class Caesar extends Cryptable {

	public Caesar() {
		super("caesar");
	}

	@Override
	public String encrypt(String object, String pass) {
		return this.encrypt(object, pass, null);
	}

	@Override
	public String encrypt(String object, String pass, Charset charset) {
		return this.cipher(object, pass.length());
	}

	@Override
	public String decrypt(String object, String pass) {
		return this.decrypt(object, pass, null);
	}

	@Override
	public String decrypt(String object, String pass, Charset charset) {
		return this.cipher(object, -pass.length());
	}
	
	public String cipher(String msg, int shift){
	    String s = "";
	    int len = msg.length();
	    for(int x = 0; x < len; x++){
	        char c = (char)(msg.charAt(x) + shift);
	        if (c > 'z')
	            s += (char)(msg.charAt(x) - (26-shift));
	        else
	            s += (char)(msg.charAt(x) + shift);
	    }
	    return s;
	}

}
